package com.bicon.botu.medical.service.impl

import com.baomidou.mybatisplus.mapper.EntityWrapper
import com.baomidou.mybatisplus.service.impl.ServiceImpl
import com.bicon.botu.core.base.common.BusinessException
import com.bicon.botu.core.base.common.Constants
import com.bicon.botu.core.base.common.utils.BeanMapper
import com.bicon.botu.medical.common.constant.FoodConstants
import com.bicon.botu.medical.dao.mapper.CustomFoodMapper
import com.bicon.botu.medical.model.dto.CustomFoodComponentDTO
import com.bicon.botu.medical.model.dto.CustomFoodDetailDTO
import com.bicon.botu.medical.model.dto.FoodAppDTO
import com.bicon.botu.medical.model.dto.UnitAndRulerTypeDTO
import com.bicon.botu.medical.model.entity.CustomFood
import com.bicon.botu.medical.model.entity.CustomFoodComponent
import com.bicon.botu.medical.service.ICustomFoodService
import lombok.extern.slf4j.Slf4j
import org.apache.commons.collections.CollectionUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.math.BigDecimal
import java.util.ArrayList

/**
 *  自定义食物Service实现类
 * @author 李双喜
 * @date 2018/7/18 13:42
 */
@Service
@Slf4j
open class CustomFoodServiceImpl : ServiceImpl<CustomFoodMapper, CustomFood>(), ICustomFoodService {

    @Autowired
    open lateinit var customFoodMapper: CustomFoodMapper


    /**
     * 获取自定义食物
     *
     * @param map
     * @return
     */
    override fun getCustomFood(map: Map<String, String>): MutableList<FoodAppDTO> {
        val foodList = customFoodMapper.getCustomFood(map)
        if (!CollectionUtils.isEmpty(foodList)) {
            foodList.stream().forEach { foodAppDTO ->
                val unitAndRulerTypeDTOS = ArrayList<UnitAndRulerTypeDTO>()
                val unitAndRulerTypeDTO = UnitAndRulerTypeDTO()
                unitAndRulerTypeDTO.unit = foodAppDTO.unit
                unitAndRulerTypeDTOS.add(unitAndRulerTypeDTO)
                foodAppDTO.units = unitAndRulerTypeDTOS
                foodAppDTO.type = FoodConstants.CUSTOM_FOOD
            }
        }
        return foodList
    }



    /**
     * 获取自定义食物详情
     *
     * @param id 食物id
     * @return
     */
    override fun getCustomFoodDetail(id: String): CustomFoodDetailDTO {
        val customFoodDetailDTO = CustomFoodDetailDTO()
        val customFood = CustomFood().selectOne(EntityWrapper<CustomFood>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL)
                .eq("id", id))
        if (customFood != null) {
            customFoodDetailDTO.specifications = customFood.quantity.toString() + customFood.unit
            BeanMapper.copy(customFood, customFoodDetailDTO)
            customFoodDetailDTO.kj = BigDecimal(customFood.kcal!!).multiply(BigDecimal(FoodConstants.KCAL_CONVERSION_KJ)).toInt()
            //获取食物成分
            val componentDTOS = CustomFoodComponent().selectList(EntityWrapper<CustomFoodComponent>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL)
                    .eq("food_id", id))
            val customFoodComponentDTOS = BeanMapper.mapList(componentDTOS, CustomFoodComponentDTO::class.java)
            customFoodDetailDTO.foodComponent = customFoodComponentDTOS
            return customFoodDetailDTO
        } else {
            throw BusinessException(404, "食物不存在")
        }
    }


}